<template>
  <el-dialog
    :title="formData.id?'编辑员工':'添加员工'"
    :visible="dialogVisible"
    @close="closeFn"
  >
    <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" />
      </el-form-item>
      <el-form-item v-if="!formData.id" label="密码" prop="password">
        <el-input v-model="formData.password" type="password" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="addFn">确 定</el-button>
        <el-button size="small" @click="closeFn">取 消</el-button>
      </el-row>
    </template>

  </el-dialog>
</template>
<script>
import { addPage, PjPage } from '@/api/staff'

export default {
  props: {
    dialogVisible: {
      default: false,
      type: Boolean
    }

  },
  data() {
    return {
      formData: {
        username: '',
        email: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    // 关闭后清除表单
    closeFn() {
      this.$emit('update:dialogVisible', false)
      this.$refs.formData.resetFields()
      this.formData = {
        username: '',
        email: '',
        password: '',
        id: ''
      }
    },
    // 添加角色
    async addFn() {
      if (this.formData.id) {
        // 编辑
        const res = await PjPage(this.formData)
        console.log(71, res)
      } else {
        // 添加
        await addPage(this.formData)
      }
      this.closeFn()
      this.$emit('updateList')
    }
  }
}
</script>
<style scoped>

</style>
